package com.smfx.yxt.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.model.ClassTeachers;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.ParentReqVO;
import com.smfx.yxt.service.IClassTeachersService;
import com.smfx.yxt.service.IOrganizeGroupService;
import com.smfx.yxt.service.IParentService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.RequestUtil;
import com.smfx.yxt.view.ParentRelStudentView;
import com.smfx.yxt.view.StudentInfoView;
import com.smfx.yxt.view.TeacherInfoView;

/**
 * 用户角色控制器
 * @author huqimeng
 * @date 2017年5月21日
 */
@Controller
public class UserRoleController extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(UserRoleController.class);
	
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private IClassTeachersService classTeachersService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;
	@Autowired
	private IParentService parentService;

	/**
	 * 家长首页
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.STUDENT_ROLE)
	public String studentIndex(Model model) {
		try {
			String sid = request.getParameter("sid");
			model.addAttribute("sid", sid);
			return "student/index";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 老师首页
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.TEACHER_ROLE)
	public String teacherIndex(HttpServletResponse response, Model model){
		try {
			return "teacher/index";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 家长对应学生列表
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.USER_ROLE_URL)
	public String studentRole(Model model) {
		List<StudentInfo> list = studentService.selectByOpenid(getOpenid());
		List<StudentInfoView> listView = BeanUtils.copyAs(list, StudentInfoView.class);
		model.addAttribute("list", listView);
		return "role/studentRole";
	}
	
	/**
	 * 老师对应班级列表
	 * @param model
	 * @return
	 */
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.USER_ROLE_URL)
	public String teacherRole(Model model) {
		AppUser appUser = getAppUser();
		if(organizeGroupService.isLeaderGroup(appUser.getTeacher().getId()) > 0) {
			BaseQueryVO vo = new BaseQueryVO();
			vo.setFinishYear(getCurrentTerm().getFinishYear());
			vo.setSchoolId(appUser.getTeacher().getSchoolId());
			List<ClassTeachers> list = classTeachersService.selectClassList(vo);
			model.addAttribute("leaderList", getGradeClassList(list));
		}else {
			List<TeacherInfoView> list = teacherService.selectClazzListByOpenid(appUser.getOpenid());
			model.addAttribute("list", list);
		}
		model.addAttribute("redirectUrl", request.getParameter("redirectUrl"));
		return "role/clazzRole";
	}
	
	@RequestMapping(value = {Const.STUDENT_ROLE + "/student/unbind", Const.TEACHER_ROLE + "/student/unbind"})
	public String studentUnbind(Model model) {
		AppUser appUser = getAppUser();
		List<ParentRelStudentView> list = parentService.selectRelStudentByOpenid(appUser.getOpenid());
		model.addAttribute("list", list);
		return "teacher/studentUnbind";
	}
	
	@RequestMapping("student/unbind/submit")
	@ResponseBody Object studentUnbindSubmit(ParentReqVO vo) {
		try {
			AppUser appUser = getAppUser();
			vo.setOpenid(appUser.getOpenid());
			parentService.delByRelId(vo);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--studentUnbindSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
}
